$(function () {
    //变量抽取
    var payfee_dataGrid = $("#payfee_dataGrid");
    var payfee_form = $("#payfee_form");
    var payfee_dialog = $("#payfee_dialog");
    var product_datagrid = $("#product_datagrid");

    /* 缴费处理 数据表格 */
    payfee_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/billRecord/query.do",
        fit: true,
        columns: [[
            {field: 'holder', title: '投保人', width: 100,formatter:function (value) {
                    return value?value.clientName:"";
                }},
            {field: 'sn', title: '核保单号', width: 100},
            {field: 'totalAmount', title: '需缴金额', width: 100},
            {field: 'state', title: '缴费状态', width: 100,formatter:function (value) {
                    return value?"<span style='color: #00ee00'>申请缴费</span>":"";
                }},
            {field: 'year', title: '保险年限', width: 100},
            {field: 'pdate', title: '核保日期', width: 100,formatter:function (value) {
                    return new Date(value).toLocaleDateString();
                }},
            {field: 'item', title: '保险的车辆信息', width: 100}
        ]],
        queryParams: {
            state:"已审核待缴费"
        },
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    /* 缴费弹框 */
    payfee_dialog.dialog({
        width: 300,
        height: 340,
        buttons: "#btns",
        closed: true,
        resizable:true,
        onClose: function () {
            payfee_form.form("clear");
        }
    });
/*
    /!* 保险产品数据表格(缴费弹框中) *!/
    product_datagrid.datagrid({
        fitColumns: true,//列自适应
        url: "",
        fit: true,
        columns: [[
            {field: 'name', title: '保险名称', width: 100},
            {field: 'money', title: '保险金额', width: 100},
        ]]
    })*/

    var methodObj = {
        searchForm: function () {
            var clientName = $("[name=clientName]").val();
            var sn = $("[name=sn]").val();

            payfee_dataGrid.datagrid('load', {
                clientName:clientName,
                sn:sn
            });
        },
        save: function () {
            var row = payfee_dataGrid.datagrid("getSelected");
            payfee_form.form('submit', {
                url: "/billRecord/update.do?id="+row.id,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            payfee_dataGrid.datagrid("load");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                }
            })
            /*$.get("/billRecord/update.do", {id: row.id}, function (data) {
                if (data.success) {
                    $.messager.alert('提示', '缴费成功', 'info', function () {
                        checkOrder_dataGrid.datagrid("reload");
                    })
                } else {
                    $.messager.alert('提示', data.msg, 'error')
                }
            });*/
        },
        cancel: function () {
            payfee_dialog.dialog("close");
        },
        reload: function () {
            payfee_dataGrid.datagrid("load");
        },
        pay:function () {
            var row = payfee_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }
            payfee_form.form("load", row);//回显数据
            $("#clientName").textbox("setValue",row.holder.clientName);
            $("#company").textbox("setValue","太平洋保险");
            payfee_dialog.dialog("setTitle","缴费处理");
            payfee_dialog.dialog("open");


            /*$("#payfee_form").form('submit', {
                url: "/billRecord/update.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            checkOrder_dataGrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                }
            })*/
        }
    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
})
